package sse.ustc.cc.fragment;

import java.util.ArrayList;
import java.util.Date;

import sse.ustc.cc.activity.CourseDetailAty;
import sse.ustc.cc.activity.R;
import sse.ustc.cc.activity.DayEntryAty;
import sse.ustc.cc.custom.CustomCalendarView;
import sse.ustc.cc.custom.CustomCalendarView.OnItemClickListener;
import sse.ustc.cc.db.DBService;
import sse.ustc.cc.util.ClassInfo;
import sse.ustc.cc.util.DataString;
import sse.ustc.cc.util.SettingInfo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MonthClassInfoFrag extends Fragment {
	private Context mContext;
	private CustomCalendarView calendarView;
	private int monthNum;
	private TextView title_month_bar;
	private TextView content_bottom;
	private ArrayList<ClassInfo> classList, classListCourse;// 获取课表信息
	private ArrayList<SettingInfo> settingList, dyweekList, dyDayList;// 获取设置和当前周数信息
	private ArrayList<String> courseList;// 获取课程信息
	private ArrayList<String> setList;// 获取设置信息
	ClassInfo classInfo;
	DataString dateString = new DataString();
	Date datecurrent = new Date();
	private long   mLastTime =0,mCurTime=0;  
	
	public static MonthClassInfoFrag newInstance(int num) {
		MonthClassInfoFrag f = new MonthClassInfoFrag();
		Bundle args = new Bundle();
		args.putInt("num", num);
		f.setArguments(args);
		return f;
	}

	// 在课程详细中获取课程信息的时候用
	public ArrayList<ClassInfo> getClassList() {
		return classList;
	}

	public void setClassList(ArrayList<ClassInfo> classList) {
		this.classList = classList;
	}

	public ArrayList<ClassInfo> getClassListCourse() {
		return classListCourse;
	}

	public void setClassListCourse(ArrayList<ClassInfo> classListCourse) {
		this.classListCourse = classListCourse;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		mContext = activity;
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		monthNum = getArguments() != null ? getArguments().getInt("num") : 1;
	}

	// 更新
	public void refreshTitle() {
		String monthInfo = calendarView.getYearAndmonth();
		String[] data = monthInfo.split("-");
		title_month_bar.setText(data[1] + "    " + data[0] + "年");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		String monthInfo;
		super.onCreateView(inflater, container, savedInstanceState);
		View v = inflater.inflate(R.layout.view_month_classinfo, container,
				false);

		calendarView = (CustomCalendarView) v.findViewById(R.id.calendar);
		title_month_bar = (TextView) v.findViewById(R.id.calendarCenter);
		content_bottom = (TextView) v.findViewById(R.id.calendarBottom);

		content_bottom.setClickable(true);
		content_bottom.setFocusable(true);

		// 得到Pager制定position的月份信息
		for (int i = 0; i < monthNum; i++) {
			monthInfo = calendarView.clickRightMonth();
		}
		// 显示当天月的信息
		refreshTitle();
		/* 初始化 */
		courseList = new ArrayList<String>();// 课程列表声明
		setList = new ArrayList<String>();// 设置列表声明
		dyweekList = new ArrayList<SettingInfo>();// 动态周数声明
		dyDayList = new ArrayList<SettingInfo>();// 动态周几声明
		dyweekList.clear();// 清空
		dyDayList.clear();// 清空
		DBService ds = new DBService(mContext);// 数据库声明
		courseList = ds.selectCourse();// 选择课表信息，存放在list里面
		setList = ds.selectSetting();// 选择设置信息，存放在list里面
		// 默认当天显示的课程信息

		// final Date datecurrent =new Date();
		int week = Integer.parseInt(DataString.getWeek(datecurrent));
		int day = DataString.getWeekOfDate(datecurrent);
		classListCourse = getClassData(day, week);// 根据周几选择界面显示的课程
		// final ClassInfo classInfo;
		if (classListCourse != null && classListCourse.size() > 0) {
			for (int i = 0; i < classList.size(); i++) {
				classInfo = classList.get(i);
				String CourseString = "课程名称：" + classInfo.getClassname()
						+ "\n课程教室：" + classInfo.getClassRoom() + "\n课程教师："
						+ classInfo.getTeacher_name() + "\n第"
						+ classInfo.getWeek_begin() + "周开课" + "\n第"
						+ classInfo.getWeek_end() + "周结课";
				content_bottom.setText(CourseString);
				content_bottom.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						Toast.makeText(mContext, "hello", Toast.LENGTH_LONG)
								.show();
						Intent intent = new Intent(getActivity(),
								CourseDetailAty.class);
						ArrayList<ClassInfo> courseInfoList = getCourseInfo(classInfo
								.getClassname());
						intent.putExtra("courseinfo", courseInfoList);
						startActivity(intent);
					}

				});
			}
		} else {
			content_bottom.setText("");
		}
		calendarView.setOnItemClickListener(new OnItemClickListener() {
			//private Object mLastTime;

			@Override
			public void OnItemClick(Date date) {
				// TODO Auto-generated method stub
				// Intent intent = new Intent(getActivity(),
				// DayEntryAty.class);
				// intent.putExtra("date", date);
				// startActivity(intent);
				// Log.e("selectedStartDate",
				// ss.format(selectedStartDate)+"");
				 // TODO Auto-generated method stub  
                mLastTime = mCurTime;  
                mCurTime = System.currentTimeMillis();  
                if (mCurTime - mLastTime < 1000) {  
					// Toast.makeText(mContext,
					// "这就是传说中的双击事件",Toast.LENGTH_SHORT).show();
                 	 Intent intent = new Intent(getActivity(),
     				 DayEntryAty.class);
     				 intent.putExtra("date", date);
     				 startActivity(intent);
                } else {  
//                	 Toast.makeText(mContext,  
//                             "这就是传说中的单击事件",Toast.LENGTH_SHORT).show();  
                	 int week = Integer.parseInt(DataString.getWeek(date));
     				int day = DataString.getWeekOfDate(date);
     				classListCourse = getClassData(day, week);// 根据周几选择界面显示的课程

     				if (classListCourse != null && classListCourse.size() > 0) {
     					for (int i = 0; i < classList.size(); i++) {
     						classInfo = classList.get(i);
     						String CourseString = "课程名称："
     								+ classInfo.getClassname() + "\n课程教室："
     								+ classInfo.getClassRoom() + "\n课程教师："
     								+ classInfo.getTeacher_name() + "\n第"
     								+ classInfo.getWeek_begin() + "周开课" + "\n第"
     								+ classInfo.getWeek_end() + "周结课";
     						content_bottom.setText(CourseString);
     						content_bottom
     								.setOnClickListener(new OnClickListener() {
     									public void onClick(View v) {
     										Toast.makeText(mContext, "hello",
     												Toast.LENGTH_LONG).show();
     										Intent intent = new Intent(
     												getActivity(),
     												CourseDetailAty.class);
     										ArrayList<ClassInfo> courseInfoList = getCourseInfo(classInfo
     												.getClassname());
     										intent.putExtra("courseinfo",
     												courseInfoList);
     										startActivity(intent);
     									}
     								});
     					}
     				} else {
     					content_bottom.setText("");
     				}

                }  
				
				// content_bottom.setText(CourseString);
				Log.e("getWeek", DataString.getWeek(date));
				Log.e("getWeekOfDate", DataString.getWeekOfDate(date) + "");
				Log.e("getSeqWeek", DataString.getSeqWeek(date) + "");

			}

		});

		return v;
	}

	/**
	 * 根据今天是星期几,所在的周数来提获取课程信息表内容
	 * 
	 * @param 参数为星期几
	 *            ，周数
	 */
	private ArrayList<ClassInfo> getClassData(int day, int week) {
		classList = new ArrayList<ClassInfo>();
		int j = courseList.size() / 13;// 确定有几门课程
		for (int i = 0; i < j; i++) {
			/* 显示当前周数有课的课程信息 */
			if (Integer.parseInt(courseList.get(13 * i + 1)) == day
					&& Integer.parseInt(courseList.get(13 * i + 10)) >= week
					&& week >= Integer.parseInt(courseList.get(13 * i + 3))) {
				ClassInfo calssInfo = new ClassInfo();
				calssInfo.setClassname(courseList.get(13 * i));// 获取课程名称
				calssInfo.setClassRoom(courseList.get(13 * i + 8));// 获取课程的教室
				calssInfo.setTeacher_name(courseList.get(13 * i + 4));

				calssInfo.setWeek_begin(courseList.get(13 * i + 3));// 获取周数结束时间
				calssInfo.setWeek_end(courseList.get(13 * i + 10));// 获取周数结束时间

				calssInfo.setFromClassNum(Integer.parseInt(courseList
						.get(13 * i + 2)));// 获取课程 节数的开始
				calssInfo.setWeekday(Integer.parseInt(courseList
						.get(13 * i + 1)));// 获取周几
				calssInfo.setSection_end(courseList.get(13 * i + 9));// 获取课程
																		// 节数的结束
				calssInfo.setSection_begin(courseList.get(13 * i + 2));// 获取课程
																		// 节数的结束

				calssInfo.setClassNumLen(Integer.parseInt(courseList
						.get(13 * i + 9))
						- Integer.parseInt(courseList.get(13 * i + 2)) + 1);// 课程节数的间隔
				calssInfo.setCourse_begin_time(courseList.get(13 * i + 11));// 获取周数结束时间
				calssInfo.setCourse_end_time(courseList.get(13 * i + 12));// 获取周数结束时间
				Log.i("",
						">>>>>>>>>>>>>>>getWeekday" + calssInfo.getWeek_begin());
				classList.add(calssInfo);
			}

		}
		return classList;

	}

	/**
	 * 获得与点击课程名称相同的课程信息
	 * 
	 * @param courseName
	 * @return
	 */
	private ArrayList<ClassInfo> getCourseInfo(String courseName) {
		ArrayList<ClassInfo> courseInfoList = new ArrayList<ClassInfo>();
		for (int i = 0; i < classList.size(); i++) {
			if (courseName.equals(classList.get(i).getClassname())) {
				courseInfoList.add(classList.get(i));
			}
		}
		return courseInfoList;
	}
}
